package Java8新日期时间API;

import org.junit.Test;

import java.time.*;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;

/*
    时间API
         ZoneId:该类中包含了所有的时区信息,一个时区的ID,如 Europe/Paris
         ZonedDateTime:一个在ISO-8601日历系统时区的日期时间,如 2007-1203T10:15:30+01:00 Europe/Paris
             其中每个时区都对应着ID,地区ID都为"{区域}/{城市}"的格式,例如:Asia/Shanghai等
         Clock:使用时区提供对当前即时、日期和时间的访问的时钟
         Duration:持续时间,用于计算两个"时间"的间隔
         Period:日期间隔,用于计算两个"日期"间隔
         TemporalAdjuster:时间校正器,有时可能需要获取例如:将日期调整到"下一个工作日"等操作
         TemporalAdjusters:该类通过静态方法(firstDayOfXxx()/lastDayOfXxx()/nextXxx())提供了大量的常用 TemporalAdjuster的实现

    与传统日期处理的转换
                类                                                           To遗留类                               FROM遗留类
        java.time.Instant与java.util.Date                                Date.from(instant)                      date.toInstant()
        java.time.Instant与java.sql.Timestamp                            Timestamp.from(instant)                 timestamp.toInstant()
        java.time.ZonedDateTime与java.util.GregorianCalendar             GregorianCalendar.from(zonedDateTime)   cal.toZonedDateTime()
        java.time.LocalDate与java.sql.Time                               Date.valueOf(localDate)                 date.toLocalDate()
        java.time.LocalTime与java.sql.Time                               Date.valueOf(localDate)                 date.toLocalTime()
        java.time.LocalDateTime与 java.sql.Timestamp                     Timestamp.valueOf(localDateTime)        timestamp.toLocalDateTime()
        java.time.ZoneId与java.util.TimeZone                             Timezone.getTimeZone(id)                timeZone.toZoneId()
        java.time.format.DateTimeFormatter与 java.text.DateFormat        formatter.toFormat()                    无
 */
public class DateTest04 {

    @Test
    public void test1() {
        //ZoneId:类中包含了所有的时区信息
        // ZoneId的getAvailableZoneIds():获取所有的ZoneId
        Set<String> zoneIds = ZoneId.getAvailableZoneIds();
        for (String s : zoneIds) {
            System.out.println(s);
        }

        // ZoneId的of():获取指定时区的时间
        LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of("Asia/Tokyo"));
        System.out.println(localDateTime);

        //ZonedDateTime:带时区的日期时间
        //ZonedDateTime的now():获取本时区的ZonedDateTime对象
        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println(zonedDateTime);

        //ZonedDateTime的now(ZoneId id):获取指定时区的ZonedDateTime对象
        ZonedDateTime zonedDateTime1 = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));
        System.out.println(zonedDateTime1);
    }

    @Test
    public void test2() {
        //Duration:用于计算两个"时间"间隔,以秒和纳秒为基准
        LocalTime localTime = LocalTime.now();
        LocalTime localTime1 = LocalTime.of(15, 23, 32);
        //between():静态方法,返回Duration对象,表示两个时间的间隔
        Duration duration = Duration.between(localTime1, localTime);
        System.out.println(duration);
        System.out.println(duration.getSeconds());
        System.out.println(duration.getNano());
        LocalDateTime localDateTime = LocalDateTime.of(2016, 6, 12, 15, 23, 32);
        LocalDateTime localDateTime1 = LocalDateTime.of(2017, 6, 12, 15, 23, 32);
        Duration duration1 = Duration.between(localDateTime1, localDateTime);
        System.out.println(duration1.toDays());
    }

    @Test
    public void test3() {
        //Period:用于计算两个"日期"间隔,以年、月、日衡量
        LocalDate localDate = LocalDate.now();
        LocalDate localDate1 = LocalDate.of(2028, 3, 18);
        Period period = Period.between(localDate, localDate1);
        System.out.println(period);
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
        Period period1 = period.withYears(2);
        System.out.println(period1.getYears());
    }

    @Test
    public void test4() {
        //TemporalAdjuster:时间校正器
        //获取当前日期的下一个周日是哪天?
        TemporalAdjuster temporalAdjuster = TemporalAdjusters.next(DayOfWeek.SUNDAY);
        LocalDateTime localDateTime = LocalDateTime.now().with(temporalAdjuster);
        System.out.println(localDateTime);
        //获取下一个工作日是哪天?
        LocalDate localDate = LocalDate.now().with(new TemporalAdjuster() {
            @Override
            public Temporal adjustInto(Temporal temporal) {
                LocalDate date = (LocalDate) temporal;
                if (date.getDayOfWeek().equals(DayOfWeek.FRIDAY)) {
                    return date.plusDays(3);
                } else if (date.getDayOfWeek().equals(DayOfWeek.SATURDAY)) {
                    return date.plusDays(2);
                } else {
                    return date.plusDays(1);
                }
            }

        });
        System.out.println("下一个工作日是:" + localDate);
    }
}
